package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.insert(addressBook);
    }

    @Override
    public List<AddressBook> listByUserId(Long userId) {
       List<AddressBook> addressBooks = addressBookMapper.getByUserId(userId);
        return addressBooks;
    }

    @Override
    public void addressBookDefault(Long id) {
        Long userId = BaseContext.getCurrentId();
        addressBookMapper.setDefaultFalseByuserId(userId);
        addressBookMapper.setDefaultTrueById(id,userId);
    }

    @Override
    public AddressBook listById(Long id) {
        return addressBookMapper.getById(id);
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void deleteAddressBookById(Long id) {
        addressBookMapper.deleteById(id);
    }
}
